<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加图书 - 图书管理系统</title>
    <link rel="stylesheet" href="/librarymanagementsystem/css/main.css">
</head>
<body>
    <div class="container">
        <header>
            <h1>图书管理系统</h1>
            <nav>
                <ul>
                    <li><a href="/librarymanagementsystem/index.html">首页</a></li>
                    <li><a href="/librarymanagementsystem/book_page.html" class="active">图书管理</a></li>
                    <li><a href="/librarymanagementsystem/borrow_page.html">借阅管理</a></li>
                    <li><a href="/librarymanagementsystem/book_add.html" class="active">添加图书</a></li>
                </ul>
            </nav>
        </header>
        
        <main>
            <h2>添加图书</h2>
            
            <form id="bookForm" method="post" action="addOrUpdate.book">
                <input type="hidden" id="id" name="id">
                <input type="hidden" name="returnPage" value="book_page.html">
                
                <div class="form-section">
                    <h3>基本信息</h3>
                    
                    <div class="form-group">
                        <label for="isbn">ISBN</label>
                        <input type="text" id="isbn" name="isbn" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="title">书名</label>
                        <input type="text" id="title" name="title" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="author">作者</label>
                        <input type="text" id="author" name="author" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="publisher">出版社</label>
                        <input type="text" id="publisher" name="publisher" required>
                    </div>
                </div>
                
                <div class="form-section">
                    <h3>详细信息</h3>
                    
                    <div class="form-group">
                        <label for="publishDate">出版日期</label>
                        <input type="date" id="publishDate" name="publishDate" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="price">价格</label>
                        <input type="number" id="price" name="price" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="category">分类</label>
                        <input type="text" id="category" name="category" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="status">状态</label>
                        <select id="status" name="status" required>
                            <option value="在库">在库</option>
                            <option value="借出">借出</option>
                            <option value="丢失">丢失</option>
                            <option value="损坏">损坏</option>
                        </select>
                    </div>
                </div>
                
                <div class="form-actions">
                    <button type="submit" class="btn">保存</button>
                    <a href="/librarymanagementsystem/book_page.html" class="btn">取消</a>
                </div>
            </form>
        </main>
        
        <footer>
            <p>&copy; 2025 图书管理系统</p>
        </footer>
    </div>
    
    <script src="/librarymanagementsystem/js/jquery-3.2.1.js"></script>
    <script src="/librarymanagementsystem/js/book.js"></script>
    <script>
        $(document).ready(function() {
            // 从URL获取图书ID参数
            const urlParams = new URLSearchParams(window.location.search);
            const bookId = urlParams.get('id');
            
            // 设置表单标题
            if (bookId) {
                $('h2').text('编辑图书');
                // 加载图书信息
                loadBookById(bookId);
            } else {
                $('h2').text('添加图书');
            }
            
            // 表单提交前验证
            $('#bookForm').on('submit', function(e) {
                if (!validateForm()) {
                    e.preventDefault();
                }
            });
        });
        
        // 表单验证
        function validateForm() {
            let isValid = true;
            
            // ISBN验证
            const isbn = $('#isbn').val().trim();
            if (!isbn) {
                alert('请输入ISBN');
                $('#isbn').focus();
                isValid = false;
            }
            
            // 书名验证
            const title = $('#title').val().trim();
            if (!title) {
                alert('请输入书名');
                $('#title').focus();
                isValid = false;
            }
            
            // 作者验证
            const author = $('#author').val().trim();
            if (!author) {
                alert('请输入作者');
                $('#author').focus();
                isValid = false;
            }
            
            // 出版社验证
            const publisher = $('#publisher').val().trim();
            if (!publisher) {
                alert('请输入出版社');
                $('#publisher').focus();
                isValid = false;
            }
            
            // 日期验证
            const publishDate = $('#publishDate').val();
            if (!publishDate) {
                alert('请选择出版日期');
                $('#publishDate').focus();
                isValid = false;
            }
            
            // 价格验证
            const price = $('#price').val();
            if (!price || isNaN(price) || price <= 0) {
                alert('请输入有效的价格');
                $('#price').focus();
                isValid = false;
            }
            
            // 分类验证
            const category = $('#category').val().trim();
            if (!category) {
                alert('请输入分类');
                $('#category').focus();
                isValid = false;
            }
            
            return isValid;
        }
    </script>
</body>
</html> 